package calao.plan.client;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.IsSerializable;

public class Resource
implements IsSerializable
{

	int id;
	WorkPeriod[] workPeriods;
	
	transient WorkPeriod[][] compiledPeriods;

	public Resource() { }
	
	public Resource(int id)
	{
		this.id = id;
	}
	
	public int getId()
	{
		return id;
	}
	
	public WorkPeriod[] getWorkPeriods()
	{
		return workPeriods;
	}
	
	public void setWorkPeriods(WorkPeriod[] periods)
	{
		this.workPeriods = periods;
		if (compiledPeriods!=null) compiledPeriods = null;
	}
	
	public WorkPeriod[] getWorkPeriods(int weekDay)
	{
		if (compiledPeriods==null) compileWorkPeriods();
		return compiledPeriods[weekDay];
	}
	
	private void compileWorkPeriods()
	{
		if (workPeriods==null) compiledPeriods = new WorkPeriod[7][0];
		else {
			compiledPeriods = new WorkPeriod[7][];
			for (int weekday=0; weekday<7; weekday++) {
				ArrayList list = new ArrayList();
				for (int i=0; i<workPeriods.length; i++) {
					if (workPeriods[i].getWeekDay()==weekday) list.add(workPeriods[i]);
				}
				compiledPeriods[weekday] = (WorkPeriod[])list.toArray(new WorkPeriod[list.size()]);
			}	
		}
	}
	
}
